/**
 * Copyright 2009 by Christian Scheid. This file is part of the switch eclipse plugin and is licensed under the
 * GPL version 3. Please refer to the URL http://www.gnu.org/licenses/gpl-3.0.html for details.
 */
package org.digitalsoul.switchplugin.prefs.util;

import org.eclipse.swt.widgets.Event;

import org.eclipse.swt.widgets.Listener;

import org.eclipse.swt.graphics.FontData;

import org.digitalsoul.switchplugin.SwitchPlugin;
import org.eclipse.jface.wizard.IWizard;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Text;


public class UITools {

    private static Font helpFont;
    
    public static Label addHelpLabel(Composite panel, String text) {
        Label helpLabel = new Label(panel, SWT.WRAP);
        helpLabel.setText(text);
        if (helpFont == null) {
            createHelpFont(helpLabel.getFont());
        }
        helpLabel.setFont(helpFont);
        helpLabel.setForeground(panel.getDisplay().getSystemColor(SWT.COLOR_TITLE_INACTIVE_FOREGROUND));
        return helpLabel;
    }
    
    private static void createHelpFont(Font oldFont) {
        FontData[] oldFontData = oldFont.getFontData();
        FontData[] newFontData = new FontData[oldFontData.length];
        for (int i = 0; i < oldFontData.length; i++) {
            newFontData[i] = new FontData(oldFontData[i].getName(), oldFontData[i].getHeight()-1, oldFontData[i].getStyle());
        }
        helpFont = new Font(oldFont.getDevice(), newFontData);
        SwitchPlugin.getDefault().getWorkbench().getDisplay().addListener(SWT.Dispose, new Listener() {
            public void handleEvent(Event arg0) {
                helpFont.dispose();
            }
        });
    }

    public static String getComboItem(Combo combo) {
        return combo.getItem(combo.getSelectionIndex());
    }
    
    public static Text addTextField(Composite panel) {
        final Text textField = new Text(panel, SWT.BORDER);
        GridData data = new GridData(GridData.FILL_HORIZONTAL);
        data.minimumWidth = 150;
        textField.setLayoutData(data);
        return textField;
    }

    public static Button addButton(Composite mainPanel, String text) {
        Button button = new Button(mainPanel, SWT.BORDER);
        button.setText(text);
        return button;
    }

    public static Composite createDuoPanel(Composite mainPanel) {
        Composite panel = new Composite(mainPanel, SWT.NONE);
        GridLayout gridLayout = new GridLayout(2, false);
        gridLayout.marginWidth = 0;
        gridLayout.marginHeight = 0;
        panel.setLayout(gridLayout);
        GridData data = new GridData();
        data.verticalAlignment = GridData.BEGINNING;
        data.horizontalAlignment = GridData.BEGINNING;
        data.grabExcessHorizontalSpace = true;
        panel.setLayoutData(data);
        return panel;
    }

    public static Label addLabel(Composite panel, String string) {
        Label label = new Label(panel, SWT.NONE);
        label.setText(string);
        return label;
    }

    public static Object getTwoSpanGridData() {
        GridData gridData = new GridData();
        gridData.horizontalSpan = 2;
        return gridData;
    }

    public static void openWizard(IWizard wizard) {
        new WizardDialog(SwitchPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getShell(), wizard).open();
    }

    public static Composite createGridPanel(Composite parent) {
        return createGridPanel(parent, 1);
    }
        
    public static Composite createGridPanel(Composite parent, int numColumns) {
        Composite plainPanel = new Composite(parent, SWT.NONE);
        GridLayout layout = new GridLayout(numColumns, false);
        layout.marginWidth = 0;
        layout.marginTop = 0;
        layout.marginBottom = 12;
        plainPanel.setLayout(layout);
        return plainPanel;
    }

    public static void selectListAfterRemove(List list, int removedIndex) {
        int newIndex = removedIndex - 1;
        if (newIndex == -1) {
            newIndex = 0;
        }
        if (list.getItemCount() > newIndex) {
            list.select(newIndex);
        }
    }
}
